package com.xbongbong.paas.connection.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.connection.enums.EsignActionEnum;
import com.xbongbong.paas.connection.pojo.esign.dto.EsignActionDTO;
import com.xbongbong.paas.connection.service.IOpenAppService;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.appmodule.pojo.dto.AppModuleOpenDTO;
import com.xbongbong.pro.domain.entity.AppModuleEntity;
import com.xbongbong.pro.enums.ModuleAppOperateEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.saas.domain.entity.EsignTaskEntity;
import com.xbongbong.saas.model.AppModuleModel;
import com.xbongbong.saas.model.EsignTaskModel;
import com.xbongbong.saas.service.AppModuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
public class EsignForConnectionService implements IOpenAppService {

    private static final Logger LOG = LoggerFactory.getLogger(EsignForConnectionService.class);

    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private EsignTaskModel esignTaskModel;
    @Resource
    private AppModuleService appModuleService;
    @Resource
    private AppModuleModel appModuleModel;

    @Override
    public JSONObject getHeader() {
        return null;
    }

    @Override
    public void dataInit(String corpid) throws XbbException {

    }

    @Override
    public JSONObject dataInitSchedule() {
        return null;
    }

    @Override
    public BaseVO openAppApiCallbackAccept(JSONObject jsonObject) throws XbbException {

        LOG.info("-------------esign openAppApiCallbackAccept-------------:" + jsonObject);

        EsignActionDTO esignActionDTO = jsonObject.toJavaObject(EsignActionDTO.class);

        String action = esignActionDTO.getAction();
        String taskId = esignActionDTO.getTaskId();
        EsignActionEnum esignActionEnum = EsignActionEnum.getByAlias(action);
        EsignTaskEntity esignTask = esignTaskModel.getByTaskId(taskId);
        switch (esignActionEnum) {
            case SIGN:
                if (Objects.nonNull(esignTask)) {
                    String key = esignTask.getCorpid() + "_" + esignTask.getFormId() + "_" + esignTask.getDataId();
                    paasRedisHelper.removeValue(RedisPrefixConstant.ESIGN_FLOW_DETAIL, key);
                }
                break;
            case CORP_AUTH:
                AppModuleEntity appModule = appModuleModel.getByAlias(SystemAppMenuEnum.ESIGN.getAlias());
                AppModuleOpenDTO openDTO = new AppModuleOpenDTO();
                openDTO.setCorpid(esignTask.getCorpid());
                openDTO.setUserId("");
                openDTO.setPlatform(PlatFormEnum.WEB.getValue());
                openDTO.setAlias(appModule.getAlias());
                openDTO.setOperate(ModuleAppOperateEnum.START.getAlias());
                openDTO.setAppModuleId(appModule.getId());
                appModuleService.open(openDTO);
                break;
            default:
                break;
        }
        return new BaseVO();
    }

    @Override
    public void requestCheck(HttpServletRequest httpRequest, JSONObject data) throws XbbException {
        //e签宝回调接收现在没有特殊的校验
    }
}
